<?php namespace Phpcmf\Controllers\Admin;

class Config extends \Phpcmf\App
{

    public function index() {

        $data = \Phpcmf\Service::M('app')->get_config(APP_DIR);

        if (IS_AJAX_POST) {

            $post = \Phpcmf\Service::L('input')->post('data');
            \Phpcmf\Service::M('app')->save_config(APP_DIR, $post);

            $this->_json(1, dr_lang('操作成功'));
        }

        $page = intval(\Phpcmf\Service::L('input')->get('page'));

        $str = '<select  class="form-control" onchange="$(this).parent().parent().parent().find(\'.myvalue\').val($(this).parent().parent().parent().find(\'.myvalue\').val()+\'{\'+this.value+\'}\')" name="_member_field"><option value=""> -- </option>';
        $str.= '<option value="username"> '.dr_lang('会员名称').' </option>';
        $str.= '<option value="email"> '.dr_lang('会员邮箱').' </option>';
        $str.= '<option value="name"> '.dr_lang('姓名').' </option>';
        $str.= '<option value="phone"> '.dr_lang('电话').' </option>';
        // 这里要改的
        if (\Phpcmf\Service::C()->member_cache['field']) {
            foreach (\Phpcmf\Service::C()->member_cache['field'] as $field => $t) {
                $str.= '<option value="'.$field.'"> '.$t['name'].' </option>';
            }
        }
        $str.= '</select>';

        $kwfield = [
            'name' => [
                'name' => '姓名',
                'fieldname' => 'name',
            ],
            'username' => [
                'name' => '账号',
                'fieldname' => 'username',
            ],
            'email' => [
                'name' => '邮箱',
                'fieldname' => 'email',
            ],
            'phone' => [
                'name' => '手机',
                'fieldname' => 'phone',
            ],
        ];

        \Phpcmf\Service::V()->assign([
            'page' => $page,
            'data' => $data,
            'form' => dr_form_hidden(['page' => $page]),
            'menu' => \Phpcmf\Service::M('auth')->_admin_menu(
                [
                    '插件设置' => [APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/index', 'fa fa-cog'],
                    '访问插件首页' => ['blank:huiyuan/config/gohome_index', 'fa fa-send'],
                    'help' => [833],
                ]
            ),
            'field' => $str,
            'kwfield' => $kwfield + $this->member_cache['field'],
            'search_seo' => '['.dr_lang('第%s页', '{page}').'{join}][{keyword}{join}][{param}{join}]黄页{join}{SITE_NAME}',
        ]);
        \Phpcmf\Service::V()->display('config.html');
    }

    public function gohome_index() {

        dr_redirect('/index.php?s=huiyuan');
    }

}
